import { Controller, Get } from '@nestjs/common';
import { ExcelTestService } from './excel-test.service';
import { Workbook } from 'exceljs';

@Controller('excel-test')
export class ExcelTestController {
  constructor(private readonly excelTestService: ExcelTestService) {}

  @Get('get')
  async getExcelInfo() {
    const workbook = new Workbook();
    const workbook2 = await workbook.xlsx.readFile(
      './代码随想录算法训练营33期.xlsx',
    );
    workbook2.eachSheet((sheet, index1) => {
      console.log('工作表' + index1);
      sheet.eachRow((row, index2) => {
        const rowData = [];
        row.eachCell((cell, index3) => {
          rowData.push(cell.value);
        });
        console.log('行' + index2, rowData);
      });
    });
  }

  @Get('get2')
  async getExcelInfo2() {
    const workbook = new Workbook();
    const workbook2 = await workbook.xlsx.readFile(
      './代码随想录算法训练营33期.xlsx',
    );
    workbook2.eachSheet((sheet, index1) => {
      const value = sheet.getSheetValues();
      console.log(value);
    });
  }

  @Get('add')
  async createExcel() {
    const workbook = new Workbook();
    const workSheet = workbook.addWorksheet('abin111');
    workSheet.columns = [
      { header: 'ID', key: 'id', width: 20 },
      { header: '姓名', key: 'name', width: 30 },
      { header: '出生日期', key: 'birthday', width: 30 },
      { header: '手机号', key: 'phone', width: 50 },
    ];
    workSheet.eachRow((row, rowIndex) => {
      row.eachCell((cell) => {
        if (rowIndex === 1) {
          cell.style = {
            font: {
              size: 10,
              bold: true,
              color: { argb: 'ffffff' },
            },
            alignment: { vertical: 'middle', horizontal: 'center' },
            fill: {
              type: 'pattern',
              pattern: 'solid',
              fgColor: { argb: '000000' },
            },
            border: {
              top: { style: 'dashed', color: { argb: '0000ff' } },
              left: { style: 'dashed', color: { argb: '0000ff' } },
              bottom: { style: 'dashed', color: { argb: '0000ff' } },
              right: { style: 'dashed', color: { argb: '0000ff' } },
            },
          };
        } else {
          cell.style = {
            font: {
              size: 10,
              bold: true,
            },
            alignment: { vertical: 'middle', horizontal: 'left' },
            border: {
              top: { style: 'dashed', color: { argb: '0000ff' } },
              left: { style: 'dashed', color: { argb: '0000ff' } },
              bottom: { style: 'dashed', color: { argb: '0000ff' } },
              right: { style: 'dashed', color: { argb: '0000ff' } },
            },
          };
        }
      });
    });

    const data = [
      {
        id: 1,
        name: '光光',
        birthday: new Date('1994-07-07'),
        phone: '13255555555',
      },
      {
        id: 2,
        name: '东东',
        birthday: new Date('1994-04-14'),
        phone: '13222222222',
      },
      {
        id: 3,
        name: '小刚',
        birthday: new Date('1995-08-08'),
        phone: '13211111111',
      },
    ];
    workSheet.addRows(data);
    workbook.xlsx.writeFile('./abin.xlsx');
  }
}
